<?php

class jPlayerWidget extends CInputWidget
{
	public $id = 'jplayer_widget';
	public $cssId = 'jplayer_interface';
	public $s3Path = '';

	public $mediaArray;
	public $baseUrl;
	public $jplayerSkin;
	
    public function run()
    {
    	if(empty($this->s3Path))
    	{
    		$this->s3Path = Yii::app()->params['S3SERVER_PATH'];
    	}
        $clientScript = Yii::app()->getClientScript();

		$clientScript->registerScriptFile($this->baseUrl . '/extensions/jPlayer/jwplayer.js', CClientScript::POS_HEAD);
		        
        $html = '$(document).ready(function() {';
        
        foreach($this->mediaArray as $model)
        {
        	$posterImage = "";
        	switch ($model->content_status_id) {
        		case Content::STATUS_APPROVED:
        			$posterImage = '/extensions/jPlayer/media/approved.jpg';
        		break;
        		case Content::STATUS_NEW:
        			$posterImage = '/extensions/jPlayer/media/new.jpg';
        		break;   
        		case Content::STATUS_REJECTED:
        			$posterImage = '/extensions/jPlayer/media/rejected.jpg';
        		break; 
        		case Content::STATUS_PENDING:
        			$posterImage = '/extensions/jPlayer/media/pending.jpg';
        		break;     		
        	}
        	
        	/*$html .= '
        	$("#'.$this->id.$model->contentFiles->id.'").jPlayer
			({
	        	ready: function ()
        		{
		            $(this).jPlayer("setMedia", 
		            {
		            	';
        	//$mediaStream = explode('|', $model->contentFiles->file_name);
			//$html .=  $mediaStream[0] . ':"' . $this->s3Path . $mediaStream[1] . '",';        	
        	$html .=  'm4v' . ':"' . $this->s3Path . Content::getFileName($model->contentFiles->file_name).ZencoderVideoEncoder::OUTPUT_FORMAT . '",';
        	
	        $html .= '		poster: "' . $this->s3Path . Content::getFileName($model->contentFiles->file_name).'.jpg"
			            });
	        		},	
	        	    ended: function (event) {
	            		$(this).jPlayer("play", 0);
	        		},		
					supplied: "mp3, m4v, webmv", 
					solution: "html, flash",
					preload: "metadata",
					cssSelectorAncestor: "#'.$this->cssId . $model->contentFiles->id.'",
					swfPath: "' . $this->baseUrl . '/extensions/jPlayer",
				})
			    .bind($.jPlayer.event.play, function() 
			    { 
			        $(this).jPlayer("pauseOthers");
			    });
			';*/
        	/* $html .=	"if(document.getElementById(".$this->id.$model->contentFiles->id.")){        	 
			        	 	jwplayer('".$this->id.$model->contentFiles->id."').setup({
						    'flashplayer': '" . $this->baseUrl . "/extensions/jPlayer/player.swf',
						    'file': '". $this->s3Path . Content::getFileName($model->contentFiles->file_name).ZencoderVideoEncoder::OUTPUT_FORMAT . "',
						    'controlbar': 'bottom',
						    'width': '206',
						    'height': '145'
						  });
			  }";   */     
        }
        
        $html .= '});';
        	
       
        $clientScript->registerScript('playMedia', $html);   
        $clientScript->registerCssFile($this->baseUrl . '/extensions/jPlayer' . $this->jplayerSkin . 'main.css');
    }
    
    public static function renderjPlyer($mediaStream, $index)
    {
    	echo '<div id="jplayer_widget' . $index . '" class="jp-jplayer"></div>';
    	
    	echo "<script>
    		$(document).ready(function() {
    				jwplayer('jplayer_widget" . $index ."').setup({
						    'flashplayer': '" . "/js/player.swf',
						    'file': 'http://s3.amazonaws.com/files.neonfields.com/". Content::getFileName($mediaStream).ZencoderVideoEncoder::OUTPUT_FORMAT . "',
						    'controlbar': 'bottom',
						    'image' : '". Yii::app()->params['S3SERVER_PATH'] . Content::getFileName($mediaStream).".jpg". "', 
						    'width': '206',
						    'height': '180',
						    'skin': '/js/stijl.zip'						            
						  });
				 });
				 </script>";
    	
    	//echo '<div id="jcontainer">Loading the player ...</div>';
		/*echo '<div class="jp-video jp-video-270p">';
			echo '<div class="jp-type-single">';
				echo '<div id="jplayer_widget' . $index . '" class="jp-jplayer"></div>';
				echo '<div id="jplayer_interface' . $index . '" class="jp-interface">';
					echo '<div class="jp-video-play"></div>';
					echo '<ul class="jp-controls">';
					echo '<li><a href="#" class="jp-play" tabindex="1">play</a></li>';
					echo '<li><a href="#" class="jp-pause" tabindex="1">pause</a></li>';
					echo '<li><a href="#" class="jp-stop" tabindex="1">stop</a></li>';
					echo '<li><a href="#" class="jp-mute" tabindex="1">mute</a></li>';
					echo '<li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>';
					echo '</ul>';
					echo '<div class="progress-bar">';
						echo '<div class="jp-progress">';
							echo '<div class="jp-seek-bar">';
								echo '<div class="jp-play-bar"></div>';
							echo '</div>';
						echo '</div>';
					echo '</div>';
					echo '<div class="jp-volume-bar">';
						echo '<div class="jp-volume-bar-value"></div>';
					echo '</div>';
					echo '<div class="full-screen"></div>';
					echo '<div class="jp-current-time"></div>';
					echo '<div class="jp-duration"></div>';
				echo '</div>';

				echo '<div class="jp-playlist">';
					echo '<ul>';
						//echo '<li>' . $mediaStream . '</li>';
						echo '<li>' . Content::getFileName($mediaStream).'.m4v'. '</li>';
					echo '</ul>';
				echo '</div>';
			echo '</div>';
		echo '</div> ';   */	
    }
}

?>